package com.xiaobai.netease_news.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xiaobai.netease_news.R;
import com.xiaobai.netease_news.model.ImgNewExtra;
import com.xiaobai.netease_news.model.ReadRecommendData;
import com.xiaobai.netease_news.utils.ImageUniversalLoaderConfiguration;
import com.xiaobai.netease_news.utils.ViewHolder;

import java.util.List;


/**
 * Created by Android Studio
 * Project: NeteaseNews
 * Author: Jack
 * Email: jackfangqi1314@gmail.com/jackfangqi@163.com
 * Date: 2015/6/7
 */
public class ReadListAdapter extends BaseAdapter {
    private List<ReadRecommendData> readRecommendDataList;
    private Context context;
    private LayoutInflater inflater;

    private DisplayImageOptions options = ImageUniversalLoaderConfiguration.initOptions();

    public ReadListAdapter(Context context, List<ReadRecommendData> readRecommendDataList) {
        this.context = context;
        this.readRecommendDataList = readRecommendDataList;
        if (context != null) {
            inflater = LayoutInflater.from(context);
        }
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();

        Toast toast = new Toast(context);
        toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 90);
        TextView textView = new TextView(context);
        textView.setText(readRecommendDataList.get(0).getPrompt());
        textView.setBackgroundColor(Color.TRANSPARENT);
        textView.setTextColor(Color.argb(0x99, 0x21, 0x21, 0x21));
        toast.setView(textView);
        toast.show();
    }

    @Override
    public int getCount() {
        if (readRecommendDataList != null) {
            return readRecommendDataList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (readRecommendDataList != null) {
            return readRecommendDataList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    @Override
    public int getItemViewType(int position) {

        ReadRecommendData readRecommendData = readRecommendDataList.get(position);
        String template = readRecommendData.getTemplate();

        if (template.equals("normal"))
            return 0;
        else if (template.equals("pic31"))
            return 1;
        else if (template.equals("pic32"))
            return 2;
        else
            return 3;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ReadRecommendData readRecommendData = readRecommendDataList.get(position);
        String template = readRecommendData.getTemplate();
        String imgsrc = readRecommendData.getImgsrc();
        String title = readRecommendData.getTitle();
        String digest = readRecommendData.getDigest();
        String source = readRecommendData.getSource();
        int upTimes = readRecommendData.getUpTimes();

        if (convertView == null) {
            if (template.equals("normal"))
                convertView = inflater.inflate(R.layout.read_recomend_normal_layout, parent, false);
            else if (template.equals("pic1"))
                convertView = inflater.inflate(R.layout.read_recommend_pic1_layout, parent, false);
            else if (template.equals("pic31"))
                convertView = inflater.inflate(R.layout.read_recommend_pic31_layout, parent, false);
            else
                convertView = inflater.inflate(R.layout.read_recommend_pic32_layout, parent, false);
        }

        ImageView normalImg = ViewHolder.getView(convertView, R.id.read_recommend_normal_img);
        TextView normalTitle = ViewHolder.getView(convertView, R.id.read_recommend_normal_title);
        TextView normalDigest = ViewHolder.getView(convertView, R.id.read_recommend_normal_digest);
        TextView normalSource = ViewHolder.getView(convertView, R.id.read_recommend_normal_source);
        CheckBox normalSupport = ViewHolder.getView(convertView, R.id.read_recommend_normal_support);

        ImageView pic1Img = ViewHolder.getView(convertView, R.id.read_recommend_pic1_img);
        TextView pic1Title = ViewHolder.getView(convertView, R.id.read_recommend_pic1_title);
        TextView pic1Digest = ViewHolder.getView(convertView, R.id.read_recommend_pic1_digest);
        TextView pic1Source = ViewHolder.getView(convertView, R.id.read_recommend_pic1_source);
        CheckBox pic1Support = ViewHolder.getView(convertView, R.id.read_recommend_pic1_support);

        ImageView pic31Img1 = ViewHolder.getView(convertView, R.id.read_recommend_pic31_img1);
        ImageView pic31Img2 = ViewHolder.getView(convertView, R.id.read_recommend_pic31_img2);
        ImageView pic31Img3 = ViewHolder.getView(convertView, R.id.read_recommend_pic31_img3);
        TextView pic31Title = ViewHolder.getView(convertView, R.id.read_recommend_pic31_title);
        TextView pic31Digest = ViewHolder.getView(convertView, R.id.read_recommend_pic31_digest);
        TextView pic31Source = ViewHolder.getView(convertView, R.id.read_recommend_pic31_source);
        CheckBox pic31Support = ViewHolder.getView(convertView, R.id.read_recommend_pic31_support);

        ImageView pic32Img1 = ViewHolder.getView(convertView, R.id.read_recommend_pic32_img1);
        ImageView pic32Img2 = ViewHolder.getView(convertView, R.id.read_recommend_pic32_img2);
        ImageView pic32Img3 = ViewHolder.getView(convertView, R.id.read_recommend_pic32_img3);
        TextView pic32Title = ViewHolder.getView(convertView, R.id.read_recommend_pic32_title);
        TextView pic32Digest = ViewHolder.getView(convertView, R.id.read_recommend_pic32_digest);
        TextView pic32Source = ViewHolder.getView(convertView, R.id.read_recommend_pic32_source);
        CheckBox pic32Support = ViewHolder.getView(convertView, R.id.read_recommend_pic32_support);

        if (template.equals("normal")) {
            if (imgsrc != null) {
                ImageLoader.getInstance().displayImage(imgsrc, normalImg, options);
            } else {
                normalImg.setVisibility(View.GONE);
            }
            normalTitle.setText(title);
            normalDigest.setText(digest);
            normalSource.setText(source);
            normalSupport.setText(String.valueOf(upTimes));
        } else if (template.equals("pic1")) {
            ImageLoader.getInstance().displayImage(imgsrc, pic1Img, options);
            pic1Title.setText(title);

            if (digest != null) {
                pic1Digest.setText(digest);
            } else
                pic1Digest.setVisibility(View.GONE);

            pic1Source.setText(source);
            pic1Support.setText(String.valueOf(upTimes));
        } else if (template.equals("pic31")) {
            ImageLoader.getInstance().displayImage(imgsrc, pic31Img1, options);
            List<ImgNewExtra> imgNewExtras = readRecommendData.getImgNewExtras();
            ImageLoader.getInstance().displayImage(imgNewExtras.get(0).getImgsrc(), pic31Img2, options);
            ImageLoader.getInstance().displayImage(imgNewExtras.get(1).getImgsrc(), pic31Img3, options);
            pic31Title.setText(title);
            if (digest != null) {
                pic31Digest.setText(digest);
            } else
                pic31Digest.setVisibility(View.GONE);

            pic31Source.setText(source);
            pic31Support.setText(String.valueOf(upTimes));
        } else if (template.equals("pic32")) {
            ImageLoader.getInstance().displayImage(imgsrc, pic32Img1, options);
            List<ImgNewExtra> imgNewExtras = readRecommendData.getImgNewExtras();
            ImageLoader.getInstance().displayImage(imgNewExtras.get(0).getImgsrc(), pic32Img2, options);
            ImageLoader.getInstance().displayImage(imgNewExtras.get(1).getImgsrc(), pic32Img3, options);
            pic32Title.setText(title);
            if (digest != null) {
                pic32Digest.setText(digest);
            } else
                pic32Digest.setVisibility(View.GONE);

            pic32Source.setText(source);
            pic32Support.setText(String.valueOf(upTimes));
        }

        return convertView;
    }
}
